<?php
session_start();
require_once( 'student_db.php' );
 
$m_db = new M_Student();

$user_id = $_SESSION['userid'];

$cYear = $m_db->getCurrentYear();
$cTerm = $m_db->getCurrentTerm();

if (isset($_POST['m'])  &&  $_POST['m']!=""){

    if ($_POST['m']== 'load'){
	  $c_id = $_POST['cl_id'];
      $result=$m_db->loadStudent_assign($c_id);
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
			$today = date("Y-m-d");	
			$year = date("Y");
			list($tyear, $tmonth, $tday)= explode("-",$today); 
			$mnow = mktime(0, 0, 0, $tmonth, $tday, $tyear );
			$ia = 1;
            while($row = $result->FetchRow()){
				
				if ($row['birthdate']!=''){
					list($byear, $bmonth, $bday) =  explode('-',$row['birthdate']);
					list( $mmonth, $mday) =  explode('/',$row['age_master']);
					$mbirthday = mktime(0, 0, 0, $bmonth, $bday, $byear);
					
					$m_define =  mktime(0, 0, 0, $mmonth, $mday, $year );
					$mage = ($mnow - $mbirthday);
					$_age = ($m_define - $mbirthday);
					$a_y = date("Y", $_age)-1970 ." ปี ";
					$a_m = date("m", $_age)-1   . " เดือน";
					
					$u_y=date("Y", $mage)-1970 ." ปี ";
					$u_m=date("m",$mage)-1 ." เดือน";
				}
				if ($ia==1){
					$room_data = $m_db->load_room($row['init_class']);
				}
				
				$ia++;
               $data[] = array(0,$row['student_id'],$row['status_id'],$row['init_class'],$row['title_name'] ." ". $row['thai_name'] . " " .$row['thai_surname']  
			   ,$row['create_date'] ,$row['register_date'],$u_y  .' '. $u_m  , $a_y ."  " . $a_m   ,$row['status_name'],$room_data ) ;
			   
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
     
    }else if ($_POST['m']=='room'){ 
		$id=  $_POST['cid'];
		echo json_encode( $m_db->load_room($id));
	}else if ($_POST['m']=='getRoom_amt'){
		$id=  $_POST['cid'];
		$result = $m_db->load_current_amt_room($id);
		$html="";
		while($row = $result->FetchRow()){
			$html.= "<span class='help-inline' style='font-weight:bold;'>ห้อง  ".$row['room']."&nbsp;:&nbsp;&nbsp;</span>";
			$color="";
			if ($row['cnt']<$row['amount']){
					$color = 'label-success';
			}else{
				$color = 'label-important';
			}
			$html.="<span class='label ".$color."' >".$row['cnt']."/".$row['amount']."</span> ";
		}
		 
		echo  $html;
	 
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		$list = $_POST['list'];
		foreach($list as $val){
			$ar['student_id'] = $val['sid'];
			$ar['class_id']=$val['cid'];
			$ar['room']=$val['r'];
			$ar['year']=  $cYear;
			
			$is_room_Avaliable = $m_db->check_room_avaliable($ar['class_id'] , $ar['room']);
			
			if ($is_room_Avaliable > 0){
				$m_db->updateStudentStatus($val['sid'], 3, $user_id);
				$m_db->AddStudentToRoom($ar);
				$m_db->AddPaymentTermFirstTime($val['sid'],$cYear, $cTerm);
			}
		}
		echo 1;
		
    }  
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if ($_GET['m'] == 'class'){
			echo json_encode( $m_db->getClassLevel());
		}else if ($_GET['m']=='age_level'){
			echo  $m_db->getAgeLevel();
		}
	} 
   
}

function DATEDIFF($date1, $date2){
	
}
 
?>